import axios from "axios";
import { getAllRight, getChildrenByRightId } from "./get";
/* 修改数据 */
export const changeRoleState = (roleId, roleState) => {
    axios.put(`/user/roleState/${roleId}?roleState=${JSON.stringify(roleState)}`)
}

export const changeAuditState = (recordId, auditState) => {
    axios.put(`/record/auditState/${recordId}?auditState=${JSON.stringify(auditState)}`)
}

export const updateUserById = (userId, data) => {
    axios.put(`/user/${userId}?data=${JSON.stringify(data)}`)
}

export const updateRightById = (rightId, data) => {
    axios.put(`/right/${rightId}?data=${JSON.stringify(data)}`)
}

export const changeRightPermission = (rightId, pagePermission) => {
    return axios.put(`/right/pagePermission/${rightId}?pagePermission=${JSON.stringify(pagePermission)}`)
}

export const changeChildrenPermission = (childrenId, pagePermission) => {
    return axios.put(`/children/pagePermission/${childrenId}?pagePermission=${JSON.stringify(pagePermission)}`)
}

const getRightIdWithGrade2 = (rights, childrens) => {
    const list = rights.filter(r => r.pagePermission === 1 && r.grade === 2)
    console.log(list);
    console.log(childrens);
    for (let i = 0; i < list.length; i++) {
        const element = list[i];
        childrens.forEach(item => {
            if (element.key === item.key) {
                console.log(item.id);
                return item.id
            }
        })
    }
}

export const changeChildrenPermissionBehindRight = async (rightId, pagePermission) => {
    const rightRes = await getAllRight()
    const childrenRes = await getChildrenByRightId(rightId)
    if (childrenRes.data) {
        const childrens = childrenRes.data.childrens
        const rights = rightRes.data.rights
        if (pagePermission === 1) {
            // 只修改right中pagePermission为1,grade为2的children权限
            const childrenId = getRightIdWithGrade2(rights, childrens)
            console.log(childrenId);
            const res = await changeChildrenPermission(childrenId, pagePermission)
            if (res) {
                return true
            }
        } else {
            // 将所有children改为0
            childrens.forEach(async item => {
                const res = await changeChildrenPermission(item.id, pagePermission)
                if (!res) {
                    return false
                }
            })
        }
    }
    return true
}

export const updateRightByRoleId = (roleId, data) => {
    return axios.put(`/role_right/${roleId}?data=${JSON.stringify(data)}`)
}

export const updateCustomerById = (customerId, customer) => {
    return axios.put(`/customer/${customerId}?data=${JSON.stringify(customer)}`)
}

export const updateBankById = (bankId, bank) => {
    return axios.put(`/bank/${bankId}?data=${JSON.stringify(bank)}`)
}

export const updateRecordById = (recordId, record) => {
    return axios.put(`/record/${recordId}?data=${JSON.stringify(record)}`)
}

export const updateRecordAuditStateById = (recordId, data) => {
    return axios.put(`/record/auditState/${recordId}?data=${JSON.stringify(data)}`)
}

export const updateDepartment = (departmentId, data) => {
    return axios.put(`/department/${departmentId}?data=${JSON.stringify(data)}`)
}

export const updateAudit = (auditId, data) => {
    return axios.put(`/audit/${auditId}?data=${JSON.stringify(data)}`)
}

export const updateCirculation = (circulationId, data) => {
    return axios.put(`/circulation/${circulationId}?data=${JSON.stringify(data)}`)
}

export const updateChannel = (channelId, data) => {
    return axios.put(`/channel/${channelId}?data=${JSON.stringify(data)}`)
}

export const updateType = (typeId, data) => {
    return axios.put(`/type/${typeId}?data=${JSON.stringify(data)}`)
}

export const updateRisk = (riskId, data) => {
    return axios.put(`/risk/${riskId}?data=${JSON.stringify(data)}`)
}

export const updateLocation = (locationId, data) => {
    return axios.put(`/location/${locationId}?data=${JSON.stringify(data)}`)
}



export const updateCategory = (categoryId, data) => {
    return axios.put(`/category/${categoryId}?data=${JSON.stringify(data)}`)
}

export const updateService = (serviceId, data) => {
    return axios.put(`/service/${serviceId}?data=${JSON.stringify(data)}`)
}

export const updateCategoryService = (id, data) => {
    return axios.put(`/category_service/${id}?data=${JSON.stringify(data)}`)
}

export const updateLoginUserById = (userId, data) => {
    return axios.put(`/login/${userId}?data=${JSON.stringify(data)}`)
}